<?php


namespace app\service\utils;


class TopicService
{
    /**
     * @param string $type 题目类型 分为A1,A3,B1,A2,B2,C1,C2,C3,D,E,F 默认C1
     * @param int $subject 科目类别 1为科目一 4为科目四 默认1
     * @param  string $chapter 章节 科目一为1-4 科目四为1-7
     * @param int $page 当前页数
     * @param int $limit 每页数量 默认1
     * @param string $sort 排序方式 正常排序normal 随机排序rand 默认normal
     * @Document https://market.cloud.tencent.com/products/11611
     * @return mixed
     * @throws \Exception
     */
    public function getTopicList($type = 'C1', $subject = 1, $chapter = '', $page = 1, $limit = 10, $sort = 'normal')
    {
        try {
            // 云市场分配的密钥Id
            $secretId = 'AKIDOwJ4W08L57qXEDC8KwbcxuoHKrDF0ajZk5wC';
            // 云市场分配的密钥Key
            $secretKey = 'CkJB9a776D1dkJTBcOMY6bI9vgvRI77wD1BTtN1y';
            $source = 'market';

            // 签名
            $datetime = gmdate('D, d M Y H:i:s T');
            $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
            $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
            $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);

            // 请求方法
            $method = 'GET';
            // 请求头
            $headers = array(
                'X-Source' => $source,
                'X-Date' => $datetime,
                'Authorization' => $auth,

            );
            // 查询参数
            $queryParams = array(
                'chapter' => $chapter,
                'pagenum' => $page,
                'pagesize' => $limit,
                'sort' => $sort,
                'subject' => $subject,
                'type' => $type,
            );
            // body参数（POST方法下）
            $bodyParams = array();
            // url参数拼接
            $url = 'https://service-7pmtx24n-1257101137.ap-shanghai.apigateway.myqcloud.com/release/driverexam/query';
            if (count($queryParams) > 0) {
                $url .= '?' . http_build_query($queryParams);
            }

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function ($v, $k) {
                return $k . ': ' . $v;
            }, array_values($headers), array_keys($headers)));
            if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
            }

            $data = curl_exec($ch);
            if (curl_errno($ch)) {
                throw new \Exception(curl_error($ch));
            }
            curl_close($ch);
            $data = json_decode($data, true);
            if ($data['status'] != 0) {
                throw new \Exception($data['msg']);
            }
            return $data['result'];
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }
}